from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
from django.views.generic import View
# Create your views here.
from blog.models import User, Car
from graduate import settings

def index(request):
    is_login = request.session.get('is_login', False)
    if is_login:
        return render(request, 'index.html', {'is_login': True})
    else:
        # 如果会话中未设置 is_login，则将其初始化为 False。
        request.session['is_login'] = False
        return render(request, 'index.html', {'is_login': False})

def news(request):
    return render(request, 'news.html')


def publicpraise(request):
    return render(request, 'publicpraise.html')

def newcar(request):
    return render(request, 'newcar.html')


def cardetail(request):
# def cardetail(request, car_id):
    # car = Car.objects.get(id=car_id)
    car = Car.objects.all()
    return render(request, 'car_detail.html', {'car': car})

class AboutcarView(View):
    def get(self, request):
        car = Car.objects.all()
        return render(request, 'aboutcar.html',locals())
    def post(self, request):
        car_id = request.POST.get('carId')
        print(car_id)
        user_id = request.session['id']
        car = Car.objects.get(id=car_id)
        user = User.objects.get(id=user_id)
        car.handle.add(user)
        return JsonResponse({'message': '添加收藏成功'})